/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.LotteryLogDao;
import com.boxin.ims.modules.wechat.entity.LotteryLog;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 抽奖日志Service
 * @author Jakemanse
 * @version 2013-09-30
 */
@Component
@Transactional(readOnly = true)
public class LotteryLogService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(LotteryLogService.class);
	
	@Autowired
	private LotteryLogDao lotteryLogDao;
	
	public LotteryLog get(Long id) {
		return lotteryLogDao.findOne(id);
	}
	
	public List<LotteryLog> findByMemberIdAndDate(Long userId ,Long memberId,Date date){
		DetachedCriteria dc = lotteryLogDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("member.id", memberId));
		dc.add(Restrictions.gt("createTime", date));
		
		return lotteryLogDao.find(dc);
	}
	
	public Page<LotteryLog> find(Page<LotteryLog> page, LotteryLog lotteryLog) {
		DetachedCriteria dc = lotteryLogDao.createDetachedCriteria();
		if (lotteryLog.getUser()!=null && lotteryLog.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", lotteryLog.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(lotteryLog.getName())){
			dc.add(Restrictions.like("name", "%"+lotteryLog.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(lotteryLog.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+lotteryLog.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", LotteryLog.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return lotteryLogDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(LotteryLog lotteryLog) {
		lotteryLogDao.save(lotteryLog);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		lotteryLogDao.deleteById(id);
	}
	
}
